package com.timeshare.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.timeshare.common.Result;
import com.timeshare.dto.AppointmentRequest;
import com.timeshare.dto.AppointmentVO;
import com.timeshare.service.AppointmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * 预约控制器
 */
@Tag(name = "预约管理")
@RestController
@RequestMapping("/appointment")
public class AppointmentController {
    
    private final AppointmentService appointmentService;
    
    public AppointmentController(AppointmentService appointmentService) {
        this.appointmentService = appointmentService;
    }
    
    @Operation(summary = "创建预约")
    @PostMapping("/create")
    public Result<?> createAppointment(Authentication authentication, @Valid @RequestBody AppointmentRequest request) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            appointmentService.createAppointment(userId, request);
            return Result.success("预约成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "确认预约")
    @PostMapping("/confirm/{id}")
    public Result<?> confirmAppointment(Authentication authentication, @PathVariable Long id) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            appointmentService.confirmAppointment(userId, id);
            return Result.success("确认成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "拒绝预约")
    @PostMapping("/reject/{id}")
    public Result<?> rejectAppointment(Authentication authentication, @PathVariable Long id) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            appointmentService.rejectAppointment(userId, id);
            return Result.success("已拒绝");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "取消预约")
    @PostMapping("/cancel/{id}")
    public Result<?> cancelAppointment(Authentication authentication, @PathVariable Long id) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            appointmentService.cancelAppointment(userId, id);
            return Result.success("已取消");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "开始预约")
    @PostMapping("/start/{id}")
    public Result<?> startAppointment(Authentication authentication, @PathVariable Long id) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            appointmentService.startAppointment(userId, id);
            return Result.success("已开始");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "完成预约")
    @PostMapping("/complete/{id}")
    public Result<?> completeAppointment(Authentication authentication, @PathVariable Long id) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            appointmentService.completeAppointment(userId, id);
            return Result.success("已完成");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取我发起的预约")
    @GetMapping("/my-requests")
    public Result<Page<AppointmentVO>> getMyRequests(
            Authentication authentication,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            Page<AppointmentVO> result = appointmentService.getMyRequests(userId, page, size);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取我收到的预约")
    @GetMapping("/my-received")
    public Result<Page<AppointmentVO>> getMyReceived(
            Authentication authentication,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            Page<AppointmentVO> result = appointmentService.getMyReceived(userId, page, size);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取预约详情")
    @GetMapping("/detail/{id}")
    public Result<AppointmentVO> getAppointmentDetail(@PathVariable Long id) {
        try {
            AppointmentVO appointment = appointmentService.getAppointmentDetail(id);
            return Result.success(appointment);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

